﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MxitAPI;

namespace MxitGUI {
    public partial class ChatControl : UserControl {

        private ChatSession chatSession;// { get; set; }
        private Contacts contacts;// { get; set; }

        private bool isEnterKey;

        private Font defaultFont;
        private float defaultFontSize;

        public TabControl TabControl { get; set; }
        public TabPage TabPage { get; set; }

        public ChatControl( ChatSession chatSession, Contacts contacts ) {
            InitializeComponent();

            FontStyle fontStyle = FontStyle.Regular;
            defaultFontSize = 10;
            defaultFont = new Font( FontFamily.GenericSansSerif, defaultFontSize, fontStyle );
            rTxtChat.Font = defaultFont;

            isEnterKey = false;
            txtMessageToSend.AcceptsReturn = false;
            txtMessageToSend.Clear();

            this.chatSession = chatSession;
            this.contacts = contacts;

            Text = contacts.GetContact( chatSession.ContactId ).NickName;
            chatSession.NewMessagesReceived += new ChatSessionNewMessageEventHandler( HandleNewMessagesReceived );

            HandleNewMessagesReceived( chatSession, null );
        }

        public void HandleNewMessagesReceived ( ChatSession chatSession, EventArgs e ) {
            if ( InvokeRequired ) {
                Invoke( new ChatSessionNewMessageEventHandler( HandleNewMessagesReceived ), chatSession, null );
                return;
            }

            while ( chatSession.NewMessagesExist() ) {
                MxitAPI.MxitMessage message = chatSession.GetNextNewMessage();
                DisplayMessage( message );
            }
        }

        public void MessageSendHandler( Object sender, EventArgs e ) {
            // Todo get flags working
            ( new SendMessageHandler( chatSession.ContactId, MessageType.Normal, txtMessageToSend.Text, "512" ) ).Execute();
            SentMessage message = new SentMessage();
            message.ContactId = chatSession.ContactId;
            message.MessageText = txtMessageToSend.Text;

            DisplayMessage( message );
            txtMessageToSend.Clear(); 
            txtMessageToSend.Focus();

        }

        private bool IsSet( Enum input, Enum matchTo ) {
            return ( Convert.ToUInt32( input ) & Convert.ToUInt32( matchTo ) ) != 0;
        }

        private void DisplayName( MxitMessage message ) {
            if ( message is ReceivedMessage )
                rTxtChat.AppendText( Contacts.Instance.GetContact( message.ContactId ).NickName + " : " );
            else if ( message is SentMessage )
                rTxtChat.AppendText( UserProfile.Instance.Name + " : " );
        }

        private void DisplayText( TextMessageTextToken token ) {
            FontStyle fontStyle = FontStyle.Regular;
            /*
            if ( TextProperties.Bold == ( token.TextProperties & TextProperties.Bold ) )
                fontStyle |= FontStyle.Bold;
            if ( ( token.TextProperties & TextProperties.Italic ) != TextProperties.Italic  )
                fontStyle |= FontStyle.Italic;
            if ( ( token.TextProperties & TextProperties.Underline ) != TextProperties.Underline )
                fontStyle |= FontStyle.Underline;
            */


            if ( IsSet( token.TextProperties, TextProperties.Bold ) )
                fontStyle |= FontStyle.Bold;
            if ( IsSet( token.TextProperties, TextProperties.Italic ) )
                fontStyle |= FontStyle.Italic;
            if ( IsSet( token.TextProperties, TextProperties.Underline ) )
                fontStyle |= FontStyle.Underline;

            float fontSize = token.TextSize;

            float newFontSize = defaultFontSize + fontSize;
            if ( newFontSize < 6 )
                newFontSize = 6;
            Font font = new Font( FontFamily.GenericSansSerif, newFontSize , fontStyle );
            rTxtChat.SelectionFont = font;

            rTxtChat.AppendText( token.Text );
            rTxtChat.ScrollToCaret();

            rTxtChat.SelectionFont = defaultFont;
        }

        public void DisplayMessage( MxitMessage message ) {

            DisplayName( message );

            MessageTextTokeniser tokeniser = new MessageTextTokeniser( message.MessageText );
            while ( tokeniser.HasNext() ){
                MessageTextToken token = tokeniser.Next();
                switch ( token.TokenType ) {
                    case TokenType.Text:
                        DisplayText( ( TextMessageTextToken  ) token );
                        break;
                }
            }

            rTxtChat.AppendText( Environment.NewLine );


            //rTxtChat.AppendText( Contacts.Instance.GetContact( message.ContactId ).NickName + " : " + message.MessageText );
            //rTxtChat.ScrollToCaret();

           // htmlChatBox.
        }

        private void txtMessageToSend_KeyPress( object sender, KeyPressEventArgs e ) {
            if ( isEnterKey ) {
                e.Handled = true;
                isEnterKey = false;
            }
            
        }

        private void txtMessageToSend_KeyDown( object sender, KeyEventArgs e ) {
            if ( e.KeyCode == Keys.Enter ) {
                MessageSendHandler( sender, e );
                isEnterKey = true;
            }

        }

        private void btnClose_Click( object sender, EventArgs e ) {
            //TabPage.
            TabControl.TabPages.Remove( TabPage );
        }


    }
}
